Completed
Push — dev ( 41defb...db632c )
by Fike
36s
created

module.exports   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
nc 8
dl 0
loc 8
rs 9.2
c 1
b 0
f 0
nop 1
1
var Slf4j = require('@ama-team/voxengine-sdk').Logger.Slf4j
2
3
module.exports = {
4
  /**
5
   * @param {LoggerOptions} [options]
6
   *
7
   * @return {Function}
8
   */
9
  factory: function (options) {
10
    var name = 'ama-team.vsf.schema.defaults.deserializer'
11
    var logger = Slf4j.factory(options, name)
12
    return function (input) {
13
      if (!input || input === '') {
14
        logger.debug('Provided input is a falsey value, returning empty object')
15
        return {}
16
      }
17
      if (typeof input !== 'string') {
18
        logger.debug('Provided input is not a string, returning object with ' +
19
          'input as $.customData')
20
        return {customData: input}
21
      }
22
      try {
23
        return JSON.parse(input)
24
      } catch (e) {
25
        logger.warn('Failed to deserialize JSON from {}, returning object ' +
26
          'with input as $.customData', input)
27
        return {customData: input}
28
      }
29
    }
30
  }
31
}
32